#include "begin.h"
#include "ui_begin.h"
#include <QMessageBox>
#include <iostream>
using namespace std;
QTimer *timer=new QTimer();
Begin::Begin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Begin)
{
    ui->setupUi(this);
    frame =new QFrame(this);
    createBlocks();
    drawLine();
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

    connect(timer,SIGNAL(timeout()),this,SLOT(timeup()));
    timer->start(1000);
    this->setWindowIcon(QIcon(":/image/menu.jpeg"));
}

void Begin::createBlocks()
{
        QColor color;
        QRgb rgb=qRgb(255, 255, 0);
        color.setRgb(rgb);
        for(int i=0;i<Max;i++)
        {
            for(int j=0;j<Max;j++)
            {
                blocks[i][j]=new Block(frame);
                blocks[i][j]->setStyleSheet("background-image:url(:/image/gg.png)");
                blocks[i][j]->setPos(i,j);
                blocks[i][j]->move(j*50,i*50);
                blocks[i][j]->changeColor(color);

            }
        }


        int w=(50)*9+1;
        frame->setMinimumSize(w,w);


}
void Begin::level_1()
{
    int i,j;
    int num1[9][9]={{-1,-1,8,3,-1,9,1,-1,-1},
                 {9,-1,-1,-1,6,-1,-1,-1,4},
                 {-1,-1,7,5,-1,4,8,-1,-1},
                 {-1,3,6,-1,-1,-1,5,4,-1},
                 {-1,-1,1,-1,-1,-1,6,-1,-1},
                 {-1,4,2,-1,-1,-1,9,7,-1},
                 {-1,-1,5,9,-1,7,3,-1,-1},
                 {6,-1,-1,-1,1,-1,-1,-1,8},
                 {-1,-1,4,6,-1,8,2,-1,-1}};
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            blocks[i][j]->setValue(num1[i][j]);
            cerr <<blocks[i][j]->data()<< " ";
           if(blocks[i][j]->data()!=-1)
           {
               blocks[i][j]->setDisabled(true);
               blocks[i][j]->changeColor(Qt::green);
           }
        }
        cerr<<endl;
    }

}
void Begin::level_2()
{
     int i,j;
     int num2[9][9]={{8,9,-1,1,7,-1,-1,-1,-1},
                     {6,-1,4,9,-1,-1,-1,8,-1},
                     {-1,-1,-1,-1,-1,-1,6,-1,-1},
                     {-1,-1,-1,-1,-1,2,4,3,-1},
                     {-1,3,-1,4,-1,8,-1,1,-1},
                     {-1,1,2,7,-1,-1,-1,-1,-1},
                     {-1,-1,9,-1,-1,-1,-1,-1,-1},
                     {-1,5,-1,-1,-1,7,9,-1,4},
                     {-1,-1,-1,-1,6,9,-1,2,7}};
     for(i=0;i<9;i++)
         for(j=0;j<9;j++)
         {
             blocks[i][j]->setValue(num2[i][j]);
            if(blocks[i][j]->data()!=-1)
            {
                blocks[i][j]->setDisabled(true);
                blocks[i][j]->changeColor(Qt::green);
            }
         }
}
void Begin::level_3()
{
    int i,j;
    int num3[9][9]={{-1,-1,-1,-1,4,-1,-1,-1,3},
                   {-1,6,-1,-1,-1,-1,9,4,-1},
                   {3,-1,4,5,-1,8,1,-1,-1},
                   {-1,2,-1,4,-1,-1,-1,-1,-1},
                   {4,3,6,-1,-1,-1,5,9,7},
                   {-1,-1,-1,-1,-1,5,-1,8,-1},
                   {-1,-1,5,1,-1,9,2,-1,8},
                   {-1,7,3,-1,-1,-1,-1,1,-1},
                   {2,-1,-1,-1,6,-1,-1,-1,-1}};
    for(i=0;i<9;i++)
        for(j=0;j<9;j++)
        {
            blocks[i][j]->setValue(num3[i][j]);
           if(blocks[i][j]->data()!=-1)
           {
               blocks[i][j]->setDisabled(true);
               blocks[i][j]->changeColor(Qt::green);
           }
        }
}
void Begin::level_4()
{
     int i,j;
     int num4[9][9]={  {-1,6,3,-1,-1,-1,7,-1,-1},
                       {-1,-1,-1,6,9,-1,-1,-1,8},
                       {9,-1,-1,-1,-1,7,-1,-1,2},
                       {-1,-1,2,-1,1,-1,-1,8,-1},
                       {-1,5,-1,8,-1,6,-1,9,-1},
                       {-1,9,-1,-1,7,-1,2,-1,-1},
                       {6,-1,-1,1,-1,-1,-1,-1,3},
                       {7,-1,-1,-1,4,5,-1,-1,-1},
                       {-1,-1,9,-1,-1,-1,1,4,-1}};
     for(i=0;i<9;i++)
         for(j=0;j<9;j++)
         {
             blocks[i][j]->setValue(num4[i][j]);
            if(blocks[i][j]->data()!=-1)
            {
                blocks[i][j]->setDisabled(true);
                blocks[i][j]->changeColor(Qt::green);
            }
         }
}
void Begin::level_5()
{
     int i,j;
     int num5[9][9]={  {-1,-1,9,-1,-1,-1,-1,-1,-1},
                       {-1,-1,4,9,-1,2,-1,-1,-1},
                       {-1,-1,2,3,-1,-1,9,1,8},
                       {-1,3,-1,-1,8,-1,1,7,-1},
                       {-1,-1,-1,5,-1,3,-1,-1,-1},
                       {-1,9,6,-1,2,-1,-1,3,-1},
                       {6,2,1,-1,-1,5,8,-1,-1},
                       {-1,-1,-1,7,-1,8,4,-1,-1},
                       {-1,-1,-1,-1,-1,-1,6,-1,-1}};
     for(i=0;i<9;i++)
         for(j=0;j<9;j++)
         {
             blocks[i][j]->setValue(num5[i][j]);
            if(blocks[i][j]->data()!=-1)
            {
                blocks[i][j]->setDisabled(true);
                blocks[i][j]->changeColor(Qt::green);
            }
         }
}
void Begin::level_6()
{
     int i,j;
     int num6[9][9]={{-1,9,-1,-1,-1,-1,-1,2,-1},
                     {2,4,-1,-1,-1,-1,-1,8,1},
                     {-1,-1,-1,1,2,5,-1,-1,-1},
                     {-1,-1,7,-1,6,-1,8,-1,-1},
                     {-1,-1,2,7,9,8,1,-1,-1},
                     {-1,-1,4,-1,1,-1,6,-1,-1},
                     {-1,-1,-1,6,8,7,-1,-1,-1},
                     {6,5,-1,-1,-1,-1,-1,7,4},
                     {-1,7,-1,-1,-1,-1,-1,6,-1}};
     for(i=0;i<9;i++)
         for(j=0;j<9;j++)
         {
             blocks[i][j]->setValue(num6[i][j]);
            if(blocks[i][j]->data()!=-1)
            {
                blocks[i][j]->setDisabled(true);
                blocks[i][j]->changeColor(Qt::green);
            }
         }
}
void Begin::level_7()
{
     int i,j;
     int num7[9][9]={{-1,3,-1,-1,-1,9,-1,-1,7},
                     {-1,1,-1,-1,8,-1,-1,-1,-1},
                     {-1,-1,-1,1,-1,-1,-1,9,-1},
                     {-1,-1,4,9,-1,5,-1,-1,6},
                     {-1,2,-1,-1,-1,-1,-1,1,-1},
                     {5,-1,-1,6,-1,7,4,-1,-1},
                     {-1,5,-1,-1,-1,1,-1,-1,-1},
                     {-1,-1,-1,-1,4,-1,-1,2,-1},
                     {7,-1,-1,5,-1,-1,-1,3,-1}};
     for(i=0;i<9;i++)
         for(j=0;j<9;j++)
         {
             blocks[i][j]->setValue(num7[i][j]);
            if(blocks[i][j]->data()!=-1)
            {
                blocks[i][j]->setDisabled(true);
                blocks[i][j]->changeColor(Qt::green);
            }
         }
}
void Begin::level_8()
{
    int i,j;
    int num8[9][9]={{-1,-1,-1,8,-1,2,-1,-1,-1},
                    {-1,-1,-1,6,-1,-1,1,-1,8},
                    {1,-1,9,-1,-1,-1,-1,-1,4},
                    {-1,5,2,-1,-1,-1,-1,-1,-1},
                    {7,3,-1,-1,-1,-1,-1,8,6},
                    {-1,-1,-1,-1,-1,-1,5,1,-1},
                    {8,-1,-1,-1,-1,-1,6,-1,7},
                    {4,-1,5,-1,-1,8,-1,-1,-1},
                    {-1,-1,-1,7,-1,5,-1,-1,-1}};
    for(i=0;i<9;i++)
        for(j=0;j<9;j++)
        {
            blocks[i][j]->setValue(num8[i][j]);
           if(blocks[i][j]->data()!=-1)
           {
               blocks[i][j]->setDisabled(true);
               blocks[i][j]->changeColor(Qt::green);
           }
        }

}
void Begin::level_9()
{
    int i,j;
    int num9[9][9]={{5,-1,-1,-1,4,-1,-1,-1,9},
                    {-1,-1,8,1,-1,5,4,-1,-1},
                    {-1,3,-1,-1,-1,-1,-1,5,-1},
                    {-1,8,-1,3,-1,7,-1,1,-1},
                    {2,-1,-1,-1,-1,-1,-1,-1,3},
                    {-1,7,-1,6,-1,2,-1,4,-1},
                    {-1,1,-1,-1,-1,-1,-1,9,-1},
                    {-1,-1,7,2,-1,9,8,-1,-1},
                    {8,-1,-1,-1,7,-1,-1,-1,4}};
    for(i=0;i<9;i++)
        for(j=0;j<9;j++)
        {
            blocks[i][j]->setValue(num9[i][j]);
           if(blocks[i][j]->data()!=-1)
           {
               blocks[i][j]->setDisabled(true);
               blocks[i][j]->changeColor(Qt::green);
           }
        }
}
void Begin::choselevel()
{
    ui->lineEdit->setDisabled(true);
    ui->lineEdit->setAlignment(Qt::AlignCenter);
    //qDebug()<<cho;
    switch(cho)
    {
    case 0:
           ui->lineEdit->setText(tr("第一关"));
           level_1();
           break;
    case 1:
           ui->lineEdit->setText(tr("第二关"));
           level_2();
           break;
    case 2:
            ui->lineEdit->setText(tr("第三关"));
            level_3();
            break;
    case 3:
            ui->lineEdit->setText(tr("第四关"));
            level_4();
            break;
    case 4:
            ui->lineEdit->setText(tr("第五关"));
            level_5();
            break;
    case 5:
            ui->lineEdit->setText(tr("第六关"));
            level_6();
            break;
    case 6:
            ui->lineEdit->setText(tr("第七关"));
            level_7();
            break;
    case 7:
            ui->lineEdit->setText(tr("第八关"));
            level_8();
            break;
    case 8:
            ui->lineEdit->setText(tr("第九关"));
            level_9();
            break;
    default:break;

    }

}


Begin::~Begin()
{
    delete ui;
}

void Begin::on_pushButton_clicked()
{
    this->close();
}

void Begin::timeup()
{
    int i,j;
    count = 0;
    for(i = 0;i<9;i++)
    {
        for(j = 0;j<9;j++)
        {
            if(blocks[i][j]->data() != -1)
            count++;

        }
    }
    if(count != 81)
    {
    ui->lcdNumber->display(tim);
    tim++;
    }
    qDebug()<<"count:"<<count;

}



void Begin::drawLine()
{

        GLine *line_H[4];
        QColor color;
        QRgb rgb=qRgb(170,0,255);
        color.setRgb(rgb);
        for(int i=0;i<4;i++)
        {
            line_H[i]=new GLine(frame);
            line_H[i]->setPen(3,color);
            line_H[i]->draw(1,frame->height());

            if(i>0)
                 line_H[i]->move(blocks[0][i*3+1]->pos().x()+blocks[0][i*3+1]->width(),0);
            else
                line_H[i]->move(0,0);
        }

        GLine *line_V[4];
        for(int i=0;i<4;i++)
        {
            line_V[i]=new GLine(frame);
            line_V[i]->setPen(3,color);
            line_V[i]->draw(0,frame->width());
            if(i>0)
                line_V[i]->move(0,blocks[i*3-1][0]->pos().y()+blocks[i*3-1][0]->height()+20);

            else
                line_V[i]->move(0,0);
        }


}

bool Begin::dfs(int sum)
{
    int i,j;
    i=sum/9;
    j=sum%9;
    while(sum<81&&map[i][j]!=-1)
    {
        sum++;
        i=sum/9;
        j=sum%9;
    }
    i=sum/9;
    j=sum%9;
    if(sum>=81)
    {
        return 1;
    }

    for(int k=1; k<=9; k++)
    {
        if(cmps(i,j,k))
        {
            map[i][j]=k;
            fw[i][k]=1;
            fh[j][k]=1;
            fs[i/3][j/3][k]=1;
            if(dfs(sum+1))
            {
                return 1;
            }
            else
            {
                fw[i][k]=0;
                fh[j][k]=0;
                fs[i/3][j/3][k]=0;
                map[i][j]=-1;
            }

        }
    }
    return 0;
}

void Begin::init()
{
    memset(fw,0,sizeof(fw)); //设置数组的初始化值
    memset(fh,0,sizeof(fh)); //1.数组名  2.初始化的值   3.初始化的长度
    memset(fs,0,sizeof(fs));
}

bool Begin::cmps(int i, int j, int k)
{
    if(fw[i][k]>=1) return 0;
    if(fh[j][k]>=1) return 0;
    if(fs[i/3][j/3][k]>=1) return 0;
    return 1;
}

bool Begin::cmp1()
{
    int i,j,k;
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            if(fw[i][j]>1||fh[i][j]>1)
                return false;
        }
    }

    for(i=0;i<5;i++)
        for(j=0;j<5;j++)
        {
            for(k=0;k<9;k++)
                if(fs[i][j][k]>1)
                    return false;
        }
    return true;
}

void Begin::on_pushButton_2_clicked()    //重置计时
{
    tim=0;
}


void Begin::on_pushButton_4_clicked()    //显示正确答案
{
    init();

    int i,j;
    for(i=0;i<9;i++)
    {
        for(j=0;j<9;j++)
        {
            map[i][j]=blocks[i][j]->data();
            //cerr<<map[i][j]<<" ";
            if(map[i][j]!=-1)
            {
                fw[i][map[i][j]]+=1;
                fh[j][map[i][j]]+=1;
                fs[i/3][j/3][map[i][j]]+=1;
            }
            else
            {
                blocks[i][j]->changeColor(Qt::yellow);
            }
        }
        cerr<<endl;
    }
    cerr<<endl;
    if(cmp1()&&dfs(0))
    {

        if(count != 81)
        {
            while(1)
            {
                int i = qrand() % 9;
                int j = qrand() % 9;
                if(blocks[i][j]->data() == -1)
                {
                     blocks[i][j]->setValue(map[i][j]);
                     blocks[i][j]->setEna(false);
                     blocks[i][j]->changeColor(Qt::red);
                     break;
                }
            }
        }
        else if(count == 81)
        {
            int ret;
            ret = QMessageBox::question(this,"提示","恭喜你，答案正确！是否继续？",QMessageBox::Yes,QMessageBox::No);
            if(ret == QMessageBox::Yes)
            {
                on_pushButton_5_clicked();

            }
            else
            {
                this->close();
            }
        }

       /* for(i=0;i<9;i++)
        {
            for(j=0;j<9;j++)
            {
                blocks[i][j]->setValue(map[i][j]);

                blocks[i][j]->setEna(false);
            }
        }*/

    }
    else
    {
        QMessageBox::warning(this,"提示","本游戏只有唯一解，根据你所填写的数字无解，请删除你所写数字再点击！",QMessageBox::Ok);

    }
}

void Begin::on_pushButton_5_clicked()  //重新选择关卡
{
    this->close();
    level *lev1 = new level;
    lev1->show();
}
